package com.zxiw.zxedu.recyclerview.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.MotionEvent
import android.view.View
import androidx.core.view.MotionEventCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.zxiw.zxedu.lib_recyclerview.R
import com.zxiw.zxedu.recyclerview.listener.OnItemDragListener
import java.util.*


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description 继承于HelperRecyclerViewAdapter提供拖拽便捷操作的baseDragAdapter
 */
abstract class HelperRecyclerViewDragAdapter<T>(context: Context, vararg layoutId: Int) :
    HelperRecyclerViewAdapter<T>(context, *layoutId) {
    companion object {
        private const val NO_TOGGLE_VIEW = 0
    }

    var mToggleViewId = NO_TOGGLE_VIEW
    private var mItemTouchHelper: ItemTouchHelper? = null
    private var itemDragEnabled = false
    private var itemSwipeEnabled = false
    private var mOnItemDragListener: OnItemDragListener? = null
    private var mDragOnLongPress = true
    fun setToggleDragOnLongPress(longPress: Boolean) {
        mDragOnLongPress = longPress
        if (mDragOnLongPress) {
            mOnToggleViewTouchListener = null
            mOnToggleViewLongClickListener = object : View.OnLongClickListener {
                override fun onLongClick(v: View): Boolean {
                    if (itemDragEnabled) {
                        mItemTouchHelper?.startDrag(v.getTag(R.id.BaseQuickAdapter_viewholder_support) as RecyclerView.ViewHolder)
                    }
                    return true
                }
            }
        } else {
            mOnToggleViewTouchListener = object : View.OnTouchListener {
                @SuppressLint("ClickableViewAccessibility")
                override fun onTouch(v: View, event: MotionEvent?): Boolean {
                    return if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN && !mDragOnLongPress) {
                        if (itemDragEnabled) {
                            mItemTouchHelper?.startDrag(v.getTag(R.id.BaseQuickAdapter_viewholder_support) as RecyclerView.ViewHolder)
                        }
                        true
                    } else {
                        false
                    }
                }
            }
        }
    }

    private var mOnToggleViewTouchListener: View.OnTouchListener? = null
    private var mOnToggleViewLongClickListener: View.OnLongClickListener? = null
    private var mObject = Any()

    override fun onBindViewHolder(holder: BH, position: Int) {
        super.onBindViewHolder(holder, position)
        if (mItemTouchHelper != null && itemDragEnabled) {
            if (mToggleViewId != NO_TOGGLE_VIEW) {
                val toggleView: View? = (holder as BaseRecyclerViewHolder).getView(mToggleViewId)
                toggleView?.setTag(R.id.BaseQuickAdapter_viewholder_support, holder)
                if (mDragOnLongPress) {
                    toggleView?.setOnLongClickListener(mOnToggleViewLongClickListener)
                } else {
                    toggleView?.setOnTouchListener(mOnToggleViewTouchListener)
                }
            } else {
                holder.itemView.setTag(R.id.BaseQuickAdapter_viewholder_support, holder)
                holder.itemView.setOnLongClickListener(mOnToggleViewLongClickListener)
            }
        }
    }

    fun enableDragItem(itemTouchHelper: ItemTouchHelper) {
        enableDragItem(itemTouchHelper, NO_TOGGLE_VIEW, true)
    }

    fun enableDragItem(
        itemTouchHelper: ItemTouchHelper,
        toggleViewId: Int,
        dragOnLongPress: Boolean
    ) {
        itemDragEnabled = true
        mItemTouchHelper = itemTouchHelper
        mToggleViewId = toggleViewId
        setToggleDragOnLongPress(dragOnLongPress)
    }

    fun disableDragItem() {
        itemDragEnabled = false
        mItemTouchHelper = null
    }

    fun isItemDraggable(): Boolean {
        return itemDragEnabled
    }

    fun enableSwipeItem() {
        itemSwipeEnabled = true
    }

    fun disableSwipeItem() {
        itemSwipeEnabled = false
    }

    fun isItemSwipeEnable(): Boolean {
        return itemSwipeEnabled
    }

    fun setOnItemDragListener(onItemDragListener: OnItemDragListener) {
        mOnItemDragListener = onItemDragListener
    }

    fun getViewHolderPosition(viewHolder: RecyclerView.ViewHolder): Int {
        //return viewHolder.getAdapterPosition() - getHeaderViewCount() -1;
        return viewHolder.adapterPosition
    }

    fun onItemDragStart(viewHolder: RecyclerView.ViewHolder) {
        if (itemDragEnabled) {
            mOnItemDragListener?.onItemDragStart(viewHolder, getViewHolderPosition(viewHolder))
        }
    }

    fun onItemDragMoving(source: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder) {
        synchronized(mObject) {
            val from = getViewHolderPosition(source)
            val to = getViewHolderPosition(target)
            if (from < to) {
                for (i in from until to) {
                    if (i + 1 < itemCount) {
                        Collections.swap(mList, i, i + 1)
                    }
                }
            } else {
                for (i in from downTo to + 1) {
                    if (i + 1 < itemCount) {
                        Collections.swap(mList, i, i - 1)
                    }
                }
            }
            notifyItemMoved(source.adapterPosition, target.adapterPosition)
            if (itemDragEnabled) {
                mOnItemDragListener?.onItemDragMoving(source, from, target, to)
            }
        }
    }

    fun onItemDragEnd(viewHolder: RecyclerView.ViewHolder) {
        if (itemDragEnabled) {
            mOnItemDragListener?.onItemDragEnd(viewHolder, getViewHolderPosition(viewHolder))
        }
    }
}